package com.zh.note.huawei.twohundred.melon的难题;

import com.zh.note.leetcode.dp.ArraysUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zhanghuan
 * @date: 2023/8/24 19:47
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int n = sc.nextInt();
        // sc.nextLine();
        // String input = sc.nextLine();
        int n = 10;
        String input = "1 1 1 1 1 9 8 3 7 10";
        List<Integer> nums = Arrays.stream(input.split(" ")).map(Integer::valueOf).collect(Collectors.toList());
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        if (sum % 2 != 0) {
            System.out.println(-1);
            return;
        }
        int target = sum / 2;
        int res = findMinCount(nums, n, target);
        if (res == n) {
            System.out.println(-1);
        } else {
            System.out.println(res);
        }
    }

    // 背包问题
    private static int findMinCount(List<Integer> nums, int n, int target) {
        int[][] dp = new int[n + 1][target + 1];
        //初始化
        for (int j = 0; j <= target; j++) {
            dp[0][j] = n;
        }
        for (int i = 1; i <= n; i++) {
            int weight = nums.get(i - 1);
            for (int j = 1; j <= target; j++) {
                if (j < weight) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.min(dp[i - 1][j], dp[i - 1][j - weight] + 1);
                }
            }
        }
        ArraysUtils.printArray(dp);
        return dp[n][target];
    }
}
